package com.Action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.OperateImage;

import com.Entity.Club;
import com.Entity.Movie_info;
import com.Service.Movie_infoService;
import com.Service.Impl.Movie_infoServiceImpl;

public class MovieinfoAction extends BaseAction{
	
	private static final Logger logger = Logger.getLogger(ClubAction.class);
	private static final String ERRO="erro";
	private File upload;  //文件
	private String uploadContentType;  //文件类型
	private String uploadFileName;   //文件名
	
	@Autowired
	private Movie_infoService movieservice;
	@Autowired
	private Movie_infoServiceImpl movieserviceimpl;
	
	Movie_info movieinfo=new Movie_info();
	List<Movie_info> movielist=new ArrayList<Movie_info>();
	public List<Movie_info> getMovielist() {
		return movielist;
	}


	public void setMovielist(List<Movie_info> movielist) {
		this.movielist = movielist;
	}


	public Movie_info getMovieinfo() {
		return movieinfo;
	}


	public void setMovieinfo(Movie_info movieinfo) {
		this.movieinfo = movieinfo;
	}

	private String title;
	private int type;
	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}

	private String timelength;
	private String descript;
	
public String addTopic() throws Exception{
       // Users user= (Users) ServletActionContext.getRequest().getSession().getAttribute("temploginbean");
        /**
         * 将医院视频的信息保存下  
         */
        String uploadPath;
		String fileName;
		String path = ServletActionContext.getRequest().getContextPath();
		String basePath = ServletActionContext.getRequest().getScheme()+"://"+ServletActionContext.getRequest().getServerName()+":"+ServletActionContext.getRequest().getServerPort()+path+"/";
		
		//logger.info("path:"+path+"-"+"basePath:"+basePath);
		
		if(upload!=null && uploadContentType!=null && uploadFileName!=null){
		try {
			System.out.println("title");
			System.out.println(title+type+timelength+descript);
			InputStream is = new FileInputStream(upload);
			//uploadPath = ServletActionContext.getServletContext().getRealPath("\\images\\clubUpload_img")+"\\"+topic.getTopicAuthorId()+"\\topicImg";
			uploadPath = ServletActionContext.getServletContext().getRealPath("/clubUpload_img");
			System.out.println(uploadPath);
			File dirFile = new File(uploadPath);
			// 判断当前文件夹（路径）是否存在，如果不存在则创建
	        if (!dirFile.exists()) {
	        	//dirFile.mkdirs();
	        	dirFile.mkdir();
	        }
	        //为文件重命名
			fileName = java.util.UUID.randomUUID().toString();
			fileName += uploadFileName.substring(uploadFileName.length() - 4);  
			File toFile = new File(uploadPath, fileName);		
			//logger.info("图片存入本地的路径="+uploadPath+"\\"+fileName);
			OutputStream os = new FileOutputStream(toFile);
			byte[] buffer = new byte[1024];
			int length = 0;  
			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);     
			}     
			is.close();
			os.close();			
			String srcImagePath=uploadPath+"/"+fileName;
			String toImagePath=uploadPath+"/"+fileName;
			OperateImage operateimage=new OperateImage();
			operateimage.reduceImageEqualProportion(srcImagePath, toImagePath, 1);
						
			String location="211.83.105.36:554/"+title;
			System.out.println(location);
			String a=String.valueOf(type);
			movieinfo.setViewercount(0);
			movieinfo.setTimelength(timelength);
			movieinfo.setName(title);
			movieinfo.setLocation(location);
			movieinfo.setLevel(a);
			movieinfo.setDescription(descript);
			Date date=new Date();
			System.out.println(date);
			movieinfo.setCreattime(new Date());
			movieinfo.setCommentcount(0);
			movieinfo.setPicturelocation(basePath+"clubUpload_img/"+fileName);
			System.out.println(movieinfo);
			this.movieservice.saveOrUpdate(movieinfo);
		} catch (Exception e) {
			e.printStackTrace();
		}  
		} 
		return "Succ";
	}

    public String movielist(){
    	/*int id=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));*/
    	System.out.println("kaishi");
    	String sql="Select * from movie_info";
    	movielist=this.movieservice.findBySql(sql);
    	System.out.println(movielist.isEmpty()); 	
    	System.out.println(movielist.get(0));
		return "ListSucc";
    	
    }

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	

	public String getTimelength() {
		return timelength;
	}


	public void setTimelength(String timelength) {
		this.timelength = timelength;
	}


	public String getDescript() {
		return descript;
	}


	public void setDescript(String descript) {
		this.descript = descript;
	}

}
